package com.dunqi.gpm.shunqing.home.widget;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.home.bean.Phone;
import com.dunqi.gpm.shunqing.home.bean.Project;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 空 on 2018/1/30 0030.
 */

public class ProjectPhoneAndDes extends FrameLayout {

    @BindView(R.id.pro_name_view)
    NormalView proNameView;
    @BindView(R.id.pro_org_view)
    NormalView proOrgView;
    @BindView(R.id.totle_money_view)
    NormalView totleMoneyView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Project project;

    public ProjectPhoneAndDes(@NonNull Context context) {
        this(context, null);
    }

    public ProjectPhoneAndDes(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View inflate = LayoutInflater.from(context).inflate(R.layout.pro_phone_and_des, this);
        ButterKnife.bind(this, inflate);
    }

    public void setData(Project project) {
        this.project = project;
        refreshUi();
    }

    private void refreshUi() {
        int color_3ea7de = R.color.color_3ea7de;
        proNameView.setText(project.getProjectName());
        proOrgView.setText(project.getRespOrgName());
        totleMoneyView.setText(project.getTotalInvestmentAmount() + "万");
        proNameView.setTextColor(color_3ea7de);
        proOrgView.setTextColor(color_3ea7de);
        totleMoneyView.setTextColor(color_3ea7de);

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        final ArrayList<Phone> phones = new ArrayList<>();

        String leaders = project.getProjectLeaders();
        if (!TextUtils.isEmpty(leaders)) {
            String[] leads = leaders.split(";");
            for (String lead : leads) {
                String[] split = lead.split(",");
                Phone phone = new Phone(split[1], split[2], "牵头领导");
                phones.add(phone);
            }
        }

        Phone phone2 = new Phone(project.getRespOrgLeaderName(), project.getRespOrgLeaderPhone(), "责任单位领导");
        phones.add(phone2);
        Phone phone3 = new Phone(project.getBizPersonName(), project.getBizPersonPhone(), "联络员");
        phones.add(phone3);
        PhoneAdapter adapter = new PhoneAdapter(phones);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Phone phone = phones.get(position);
                call(getContext(), phone.getPhone());
            }
        });
    }

    class PhoneAdapter extends RecyclerAdapter<Phone> {
        public PhoneAdapter(@Nullable List<Phone> data) {
            super(R.layout.item_phone, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Phone item) {
            helper.setText(R.id.name_tv, item.getName())
                    .setText(R.id.job_tv, item.getJob());
        }
    }

    /**
     * 调用拨号界面
     *
     * @param phone 电话号码
     */
    private void call(Context context, String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
